﻿//////////////////////////////////////////////////////////////////////////////
//
// Copyright © 1998-2024 Glodon Company Limited.
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the “Software”),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
//////////////////////////////////////////////////////////////////////////////
#include "CmdCreateView2d.h"
#include "ICanvas.h"
#include "ICanvasEvent.h"
#include "IDocument.h"
#include "IMainWindow.h"
#include "IModelView.h"
#include "IUiDocument.h"
#include "IUiView.h"
#include "IUiViewMouseEvent.h"
#include "IUserTransaction.h"
#include "UiDocumentViewUtils.h"
#include "Vector3dUtils.h"
#include "CommandIds.h"
#include "GbmpNew.h"
#include "CommandRegister.h"
#include "ISectionBox.h"
#include "IViewClipRange.h"
#include "IElementStatus.h"
#include "OwnerPtr.h"
#include "ModelViewUtils.h"
#include "IGraphicsStyleData.h"
#include "IGraphicsStyleDataOverrideItems.h"
#include "EnableCompileWarning_The_LAST_IncludeInCpp.h" 

using namespace gcmp;
using namespace Sample;

REGISTER_COMMAND(CmdCreateView2d);
CmdCreateView2d::CmdCreateView2d()
    :gcmp::CommandBase(COMMAND_VIEW_2D)
{}

bool CmdCreateView2d::IsEnabled() const
{
    IDocument* pCurrentDoc = UiDocumentViewUtils::GetCurrentDocument();

    return  (pCurrentDoc == nullptr) ? false : true;
}

gcmp::OwnerPtr<gcmp::IAction> CmdCreateView2d::ExecuteCommand(const gcmp::CommandParameters& cmdParams)
{
    IUiDocument*  pUiDocument = UiDocumentViewUtils::GetCurrentUiDocument();
    DBG_WARN_AND_RETURN_NULLPTR_UNLESS(pUiDocument, L"请打开文档", L"GDMP", L"2023-09-30");

    IUiView* pUiView2d = CreateUiView(pUiDocument, L"");
    if (pUiView2d)
    {
        pUiView2d->SetTitle(pUiDocument->GetDbDocument()->GetName() + L" - 平面");

        pUiView2d->GetCanvas()->MarkNeedRefreshByViewScale(true);
        pUiView2d->GetCanvas()->MarkNeedRefreshLineTypeScale(true);
    }
    return nullptr;
}

gcmp::IUiView* CmdCreateView2d::CreateUiView(gcmp::IUiDocument* pUiDocument, const std::wstring viewName)
{
    IUiView* pUiViewXY = nullptr;
    //1.检查打开的视图是否有2D视图
    for each (gcmp::IUiView* pUiView in pUiDocument->GetAllUiViews())
    {
        if (pUiView->IsSubUiView())
        {
            continue;
        }

        if (gcmp::BuiltInViewType::TwoDimensional != pUiView->GetModelView()->GetViewType())
        {
            continue;
        }

        gcmp::IModelView* pModelView2d = pUiView->GetModelView();
        if (!(gcmp::Vector3dUtils::IsEqual(Vector3d(1, 0, 0), pModelView2d->GetRightDirection())&& gcmp::Vector3dUtils::IsEqual(Vector3d(0, 1, 0), pModelView2d->GetUpDirection())))
        {
            continue;
        }
        if (pUiView->GetTag() == viewName)
        {
            pUiViewXY = pUiView;
            break;
        }
    }

    //2.有的话直接打开
    if (pUiViewXY != nullptr)
    {
        if (UiDocumentViewUtils::GetCurrentUiView() != pUiViewXY)
        {
            gcmp::IMainWindow::GetMainWindow()->CreateOrOpenUiView(pUiDocument->GetId(), pUiViewXY->GetModelView()->GetElementId(), false, pUiViewXY->GetTag());
        }
        else
        {
            pUiViewXY->GetCanvas()->Refresh();
        }

        return pUiViewXY;
    }
    //3.没有的话继续检查文档是否有2D模型
    gcmp::IModelView* pModelView2d = CreateModelView(pUiDocument->GetDbDocument(), L"ModelView2d_XY");
    if (nullptr == pModelView2d)
    {
        return false;
    }

    return IMainWindow::GetMainWindow()->CreateOrOpenUiView(pUiDocument->GetId(), pModelView2d->GetElementId(), false, viewName);
}


gcmp::IModelView* CmdCreateView2d::CreateModelView(gcmp::IDocument* pDocument, const std::wstring viewName/*L"ModelView2d"*/)
{
    gcmp::IModelView* pModelView2d = nullptr;
    //查找
    for each (gcmp::IModelView* pModelView in gcmp::IModelView::GetAllModelViews(pDocument))
    {
        if (gcmp::BuiltInViewType::TwoDimensional == pModelView->GetViewType())
        {
            if (gcmp::Vector3dUtils::IsEqual(Vector3d(1, 0, 0), pModelView->GetRightDirection()) 
                && gcmp::Vector3dUtils::IsEqual(Vector3d(0, 1, 0), pModelView->GetUpDirection()))
            {
                if (pModelView->GetName() == viewName)
                {
                    pModelView2d = pModelView;
                    break;
                }
            }
        }
    }
    if (pModelView2d)
    {
        return pModelView2d;
    }
    //创建
    if (IUserTransaction::IsInUserTransaction(pDocument, nullptr))
    {
        pModelView2d = IModelView::Create(pDocument,
            viewName, BuiltInViewType::TwoDimensional, 
            Vector3d::NegaUnitZ, Vector3d::Zero, 
            Vector3d::UnitX, Vector3d::UnitY, 
            ElementCreationOptions::Normal);
        
        if (nullptr != pModelView2d)
        {
            pModelView2d->SetUserDataId(PlanViewType);
        }

        return pModelView2d;
    }

    OwnerPtr<IUserTransaction> opUserTransaction = 
        IUserTransaction::Create(pDocument, L"创建2D模型视图");
    pModelView2d = gcmp::IModelView::Create(pDocument, 
        viewName, BuiltInViewType::TwoDimensional,
        Vector3d::NegaUnitZ, Vector3d::Zero, 
        Vector3d::UnitX, Vector3d::UnitY,
        ElementCreationOptions::Normal);
    if (nullptr == pModelView2d)
    {
        opUserTransaction->Rollback();
        return nullptr;
    }
    pModelView2d->SetUserDataId(PlanViewType);
    //设置视图比例
    pModelView2d->SetViewScale(0.01);
    //设置显示详细程度
    pModelView2d->SetDetailLevel(ModelViewDetailLevel::Fine);
    //设置显示模式
    pModelView2d->SetDisplayMode(ModelViewDisplayMode::HiddenLine);
    //设置视图深度方向
    pModelView2d->AddTopPlane(2300);
    pModelView2d->AddCutPlane(1200.0);
    pModelView2d->AddBottomPlane(-150.0);
    pModelView2d->AddDeepPlane(-150.0);
    //启用裁剪框
    ElementId clipRangeId = pModelView2d->GetViewClipRangeId();
    IViewClipRange* pClipRange = quick_cast<IViewClipRange>(pDocument->GetElement(clipRangeId));
    DBG_WARN_AND_RETURN_NULLPTR_UNLESS(pClipRange, L"pClipRange指针为空", L"GDMP", L"2023-09-30");
    pClipRange->SetClipVisible(true);
    pModelView2d->CreateDefaultRectViewport(50000., 50000., 30000., 30000.);
    pModelView2d->CropByViewport(true);

    opUserTransaction->Commit();


    return pModelView2d;
}

